Text files
Text files is a kind of files that contain only ASCII code code such as letters,
numbers, and symbols. Example of text files in: .bat, .txt, .ini, .pas, and many
other readable files.
Text files in Object Pascal
Text files in Object Pascal declared as TextFile, reading, writing, and other manipulation
described below:
---------------------------------------------------------------------------------------------------------------------
Operation proc/func
----------------------------------------------------------------------------------------------------------------------
Declaration var F: TextFile;
Assigning AssignFile(F, AFileName);
Opening for reading Reset(F);
Opening for writing Rewrite(F);
Opening for read/write -
Appending Append(F);
Reading Readln(F, Line); / Read(F, Ch);
Writing Writeln(F, Line); / Write(F, Ch);
Goto random position -
Closing file CloseFile(F);
------------------------------------------------------------------------------------------------------------------------
Text file is organized as sequential file so that you can not open it in reading/writing
mode also you can not goto any record randomly.
Readln in text file means reading a line until line feed/cariage return (#10/#13)
combination encountered.
Writeln in text files writes a line of data then it adds cariage return/line feed (#13/#10)
to the end of line.
Example:
- Drop a Memo and a Button.
- At button's OnClick event write:
procedure TForm1.Button1Click(Sender: TObject);
var
F: TextFile;
Line: string;
begin
AssignFile(F, 'c:\autoexec.bat');
Reset(F);
Form1.Memo1.Lines.Clear;
while not Eof(F) do
begin
Readln(F, Line);
Form1.Memo1.Lines.Add(Line);
end; // while
CloseFile(F);
end;
Notes:
- In text files you can write and read integer types, real types, strings, characters,
and boolean.
- Records in text files (Lines) seperated by cariage return/line feed (#13#10) combination.
- Size of record (line) is not fixed, it depends on length of data.
- Text files are very effecient when using different size strings, because it stores
only actual string length.
See also:
Typed files
Untyped files